﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;
using System.Collections.Specialized;

using Cybertron_Tracker.Cybertron;

namespace Cybertron_Tracker
{
    public partial class frmMain : Form
    {
        private DateTime logStart;
        private DateTime logEnd;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            updateProjects();
            updateActivityList();
        }

        private void updateProjects()
        {
            cmbProjects.Items.Clear();
            Hashtable projects = RedMine.getProjects();

            cmbProjects.Items.Add("All");
            foreach (DictionaryEntry de in projects)
            {
                cmbProjects.Items.Add( ((Project)de.Value).name );
            }
            cmbProjects.SelectedIndex = 0;

        }

        private void updateActivityList()
        {
            Hashtable activityList = RedMine.getActivityList();

            foreach (DictionaryEntry de in activityList)
            {
                cmbActivity.Items.Add( ((Activity)de.Value).name );
            }
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void recentActivityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RedMine.getRecentActivity();
        }

        private void btnUpdateProjects_Click(object sender, EventArgs e)
        {
            updateProjects();
        }

        private void cmbProjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbProjects.Text != "")
            {
                cmbIssues.Items.Clear();
                Hashtable issues = RedMine.getIssues(cmbProjects.Text);

                cmbIssues.Items.Add("None");
                foreach (DictionaryEntry de in issues)
                {
                    cmbIssues.Items.Add(((Issue)de.Value).name);
                }
                cmbIssues.SelectedIndex = 0;
            }
        }

        private void cmbIssues_SelectedIndexChanged(object sender, EventArgs e)
        {
            Issue issue = RedMine.selectIssue(cmbIssues.Text);
            if (issue != null)
            {
                lblIssueLink.Visible = true;
                lblIssueLink.Tag = issue.link;
                webBrowser.DocumentText = Html.styles() + issue.content;
            }
            else
            {
                lblIssueLink.Visible = false;
                webBrowser.DocumentText = Html.styles() + "No issue selected.";
            }
        }

        private void btnStartlog_Click(object sender, EventArgs e)
        {
            if (RedMine.hasIssueSelected())
            {
                logStart = DateTime.Now;
                RedMine.startLog();

                tmrLog.Start();

                cmbIssues.Enabled = false;
                cmbProjects.Enabled = false;
                btnUpdateProjects.Enabled = false;

                btnStartlog.Visible = false;
                btnPauselog.Visible = true;

                notifyIcon.BalloonTipTitle = cmbProjects.Text;
                minimizeAndNotify();
            }
            else
            {
                MessageBox.Show("First select one Issue.", "Issue error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnPauselog_Click(object sender, EventArgs e)
        {
            if (cmbActivity.Text.Trim() != "")
            {
                String logComment = txtComment.Text.Trim();
                logEnd = DateTime.Now;

                RedMine.endLog(logComment, cmbActivity.Text);

                tmrLog.Stop();

                cmbIssues.Enabled = true;
                cmbProjects.Enabled = true;
                btnUpdateProjects.Enabled = true;

                btnStartlog.Visible = true;
                btnPauselog.Visible = false;

                txtComment.Text = "";

                notifyIcon.BalloonTipText = this.Text;//form title
            }
            else
            {
                MessageBox.Show("You must select the log activity.", "Activity error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            if (notifyIcon.Visible == true)
            {
                open();
            }
        }

        private void tmrLog_Tick(object sender, EventArgs e)
        {
            logEnd = DateTime.Now;
            calculateTimeDiff();
        }

        private void calculateTimeDiff()
        {
            TimeSpan diff = logEnd - logStart;

            String logTime = String.Format("{0:00}:{1:00}:{2:00}", diff.Hours, diff.Minutes, diff.Seconds);
            lblTime.Text = "Log: " + logTime;

            notifyIcon.Text = cmbProjects.Text + ": #" + ((Issue)(RedMine.getIssues(cmbProjects.Text))[cmbIssues.Text]).id + " (" + logTime + ")";
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tmrLog.Enabled == true)
            {
                btnPauselog_Click(sender, e);
            }
            if (tmrLog.Enabled == true) e.Cancel = true;
        }

        private void lblIssueLink_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start((string)lblIssueLink.Tag);
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                minimizeAndNotify();
            }
            else if (this.WindowState == FormWindowState.Normal)
            {
                notifyIcon.Visible = false;
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            open();
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            open();
        }

        private void minimizeAndNotify()
        {
            notifyIcon.Visible = true;
            notifyIcon.BalloonTipText = cmbIssues.Text;
            notifyIcon.ShowBalloonTip(500);
            this.Hide();
        }

        private void open()
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            notifyIcon.Visible = false;
        }

    }
}
